package com.spatio.serving.basic.service.template.server.controller;


import com.spatio.serving.basic.service.template.common.domain.UserLoginDo;
import com.spatio.serving.basic.service.template.common.dto.dify.DifyCommon;
import com.spatio.serving.basic.service.template.common.req.AskSimpleReq;
import com.spatio.serving.basic.service.template.common.vo.UrlUploadInfo;
import com.spatio.serving.basic.service.template.core.biz.DiaLogBiz;
import com.spatio.serving.basic.service.template.mysql.Result.Result;
import com.spatio.serving.basic.service.template.server.Interceptor.JWTInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/chat")
public class ChatController {



    @Autowired
    private DiaLogBiz diaLogBiz;


    @PostMapping("/dify")
    public Result askSimpleStream( @RequestBody @Valid AskSimpleReq request, HttpServletResponse res) {
        // The server declares that the stream of events is sent next
        res.setContentType("text/event-stream;charset=UTF-8");
        res.setHeader("Cache-Control", "no-cache");
        res.setHeader("Connection", "keep-alive");
        BigInteger userId = JWTInterceptor.getUserInfo().getUser_id();

        try {
            DifyCommon ans= diaLogBiz.askSimpleStream(userId,request, res);
            return Result.success(ans);
        } catch (Exception e) {
            log.error("ApiController DiaLogBiz ask error request:{}", request, e);
            Result.error(e.toString());
        }
        return Result.error("请再输入一次");
    }

    /**
     * 大模型对话
     * @param request
     * @param res
     */
    @PostMapping("/difyv2")
    public void askSimpleStreamV2( @RequestBody @Valid AskSimpleReq request, HttpServletResponse res) {
        // The server declares that the stream of events is sent next
        res.setContentType("text/event-stream;charset=UTF-8");
        res.setHeader("Cache-Control", "no-cache");
        res.setHeader("Connection", "keep-alive");
        Map<String,String>ss=new HashMap<>();
        BigInteger userId = JWTInterceptor.getUserInfo().getUser_id();

        try {
           diaLogBiz.askSimpleStreamV2(userId,request, res);
        } catch (Exception e) {
            log.error("ApiController DiaLogBiz ask error request:{}", request, e);
            Result.error(e.toString());
        }
    }

    /**
     * 用户上传图片
     * @param file
     * @param region
     * @param bizType
     * @param contentType
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("file") MultipartFile file,
                             @RequestParam(value = "region",defaultValue = "cn") String region,
                             @RequestParam(value = "biz_type",defaultValue = "calories-image") String bizType,
                             @RequestParam(value = "content_type",defaultValue = "image") String contentType) throws IOException {
//        String filename = "/Users/edy/IdeaProjects/spatio-template/test.wav";
//        String region  = "cn";
        UserLoginDo user= JWTInterceptor.getUserInfo();
        UrlUploadInfo info  = diaLogBiz.uploadFile(file,region,bizType,contentType);
        List<String>list=new ArrayList<>();
        list.add(info.getUrlForAccess());
        String url = "https://"+diaLogBiz.getUrl(list).keySet().iterator().next();
        return  Result.success(url);
    }

    /**
     * 获得大模型开场白
     * @return
     */
    @PostMapping("/open")
    public Result getOpen(){
        String res= diaLogBiz.getOpen();
        return Result.success(res);
    }

}
